﻿using ParaEditor.Domain.Entities;
using System.Collections.ObjectModel;
using System.Collections;
using ParaEditor.Common;
using System;
using System.Collections.Generic;
using NHibernate;

namespace ParaEditor.DAO.ParaDAO
{
    public class Para3007DAO:BaseDAO
    {
        private static Para3007 p3007 = new Para3007();
        public Para3007DAO()
            : base(p3007)
        {
        }

        public ObservableCollection<Para3007> GetObjectByMaxInfoVer()
        {
            ObservableCollection<Para3007> list = new ObservableCollection<Para3007>();
            int infoVer = base.GetMaxID();
            IList iList = base.GetObjectByInfoVer(infoVer);
            foreach (Para3007 item in iList)
            {
                list.Add(item);
            }
            return list;
        }

        public ResultValue UpdateLineInfoCnNamePointer(string nameCn, int pointer)
        {
            ResultValue rv = new ResultValue();
            string sqlQuery = "update TBL_METRO_LINE_INFO set LINE_NM_CN_POINT = " + pointer + " where trim(LINE_NM_CN) = '" + nameCn + "'";
            try
            {
                ISQLQuery Query = Session.CreateSQLQuery(sqlQuery).AddEntity(p3007.GetType());
                Query.ExecuteUpdate();
                Session.Flush();
                Session.Clear();
                rv.Code = 0;
            }
            catch (Exception e)
            {
                rv.Code = -1;
                rv.Msg = e.Message;

            }
            return rv;
        }

        public ObservableCollection<Para3007> GetObjectByInfoVer(int infoVer)
        {
            ObservableCollection<Para3007> list = new ObservableCollection<Para3007>();
            IList iList = base.GetObjectByInfoVer(infoVer);
            foreach (Para3007 item in iList)
            {
                list.Add(item);
            }
            return list;
        }

        public ResultValue BatchCreateObject(IList<Para3007> objs)
        {
            ResultValue rValue = new ResultValue();
            try
            {
                int count = objs.Count;
                for (int i = 0; i < count; i++)
                {
                    Session.Merge(objs[i]);
                    if (i % 100 == 0)
                    {
                        Session.Flush();
                        Session.Clear();
                    }
                }
                rValue.Code = 0;
            }
            catch (Exception e)
            {
                rValue.Code = Int32.Parse(ErrorFlag.SAVE_ERROR.Split(':')[0]);
                rValue.Msg = e.Message;
            }
            finally
            {
                Session.Flush();
                Session.Clear();
            }
            return rValue;
        }
    }
}
